package com.ricky.base.util;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.j256.ormlite.support.ConnectionSource;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Ricky
 * @date 2020/11/6
 */
@Slf4j
public class DaoUtil {
    /**
     * 批量插入
     *
     * @param dbPath
     * @param dataList
     * @param clazz
     * @param <E>      列表元素类型
     */
    public static <E> int insertList(String dbPath, List<E> dataList, Class<E> clazz) {
        String databaseUrl = "jdbc:sqlite:" + dbPath;
        try (ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl)) {
            Dao<E, String> dao = DaoManager.createDao(connectionSource, clazz);
            return dao.create(dataList);
        } catch (SQLException | IOException throwables) {
            throw new RuntimeException("批量插入失败", throwables);
        }
    }

    /**
     * 插入
     *
     * @param dbPath db路径
     * @param data   数据
     * @param clazz
     * @param <T>    数据类型
     */
    public static <T> int insert(String dbPath, T data, Class<T> clazz) {
        String databaseUrl = "jdbc:sqlite:" + dbPath;
        try (ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl)) {
            Dao<T, String> dao = DaoManager.createDao(connectionSource, clazz);
            return dao.create(data);
        } catch (SQLException | IOException throwables) {
            throw new RuntimeException("插入失败", throwables);
        }
    }

    /**
     * 批量插入
     *
     * @param dbPath
     * @param dataList
     * @param clazz
     * @param <E>      列表元素类型
     */
    public static <E> int deleteList(String dbPath, List<E> dataList, Class<E> clazz) {
        String databaseUrl = "jdbc:sqlite:" + dbPath;
        try (ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl)) {
            Dao<E, String> dao = DaoManager.createDao(connectionSource, clazz);
            return dao.delete(dataList);
        } catch (SQLException | IOException throwables) {
            throw new RuntimeException("批量删除失败", throwables);
        }
    }

    public static <T> int delete(String dbPath, T data, Class<T> clazz) {
        String databaseUrl = "jdbc:sqlite:" + dbPath;
        try (ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl)) {
            Dao<T, String> dao = DaoManager.createDao(connectionSource, clazz);
            return dao.delete(data);
        } catch (SQLException | IOException throwables) {
            throw new RuntimeException("删除失败", throwables);
        }
    }

    public static <T> int update(String dbPath, T data, Class<T> clazz) {
        String databaseUrl = "jdbc:sqlite:" + dbPath;
        try (ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl)) {
            Dao<T, String> dao = DaoManager.createDao(connectionSource, clazz);
            return dao.update(data);
        } catch (SQLException | IOException throwables) {
            throw new RuntimeException("更新失败", throwables);
        }
    }

    public static <T> List<T> getAll(String dbPath, Class<T> clazz) {
        String databaseUrl = "jdbc:sqlite:" + dbPath;
        try (ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl)) {
            Dao<T, String> dao = DaoManager.createDao(connectionSource, clazz);


            //            QueryBuilder shopBrandQueryBuilder = dao.queryBuilder();
            //            QueryBuilder shopQueryBuilder = dao.queryBuilder();
            //
            //            Where shopBrandWhere = shopBrandQueryBuilder.where();
            //            shopBrandWhere.eq(ShopBrand.SHOP_NO, shopNo);
            //
            //            Where shopWhere = shopQueryBuilder.where();
            //            shopWhere
            //                    .like(Shop.SHOP_CODE, "%" + serch + "%")
            //                    .or()
            //                    .like(Shop.SHOP_LNAME, "%" + serch + "%");
            //
            //            QueryBuilder resultQueryBuilder = shopBrandQueryBuilder.join(shopQueryBuilder);
            //            shopBrands = resultQueryBuilder.query();

            return dao.queryForAll();
        } catch (SQLException | IOException throwables) {
            throw new RuntimeException("查询失败", throwables);
        }
    }

    public static <T> T getById(String dbPath, String id, Class<T> clazz) {
        String databaseUrl = "jdbc:sqlite:" + dbPath;
        try (ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl)) {
            Dao<T, String> dao = DaoManager.createDao(connectionSource, clazz);
            return dao.queryForId(id);
        } catch (SQLException | IOException throwables) {
            throw new RuntimeException("查询失败", throwables);
        }
    }
}
